package com.lucaswangdev.logindemo.exception;

import com.lucaswangdev.logindemo.constant.common.ErrorInfo;
import org.apache.commons.lang3.StringUtils;

public class BusinessException extends RuntimeException {
    private String errorCode;
    private String errorMsg;

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public BusinessException(String errorCode, String errorMsg, Throwable cause) {
        super("[" + errorCode + "]" + ":" + errorMsg, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BusinessException(String errorCode, String errorMsg) {
        super("[" + errorCode + "]" + ":" + errorMsg);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BusinessException(ErrorInfo errorInfo) {
        super("[" + errorInfo.getErrorCode() + "]" + ":" + errorInfo.getErrorMsg());
        this.errorCode = errorInfo.getErrorCode();
        this.errorMsg = errorInfo.getErrorMsg();
    }

    public BusinessException(ErrorInfo errorInfo, String msg) {
        super("[" + errorInfo.getErrorCode() + "]" + ":" + errorInfo.getErrorMsg());
        this.errorCode = errorInfo.getErrorCode();
        if(StringUtils.isNotBlank(msg)) {
            this.errorMsg = errorInfo.getErrorMsg() + "(" + msg + ")";
        } else {
            this.errorMsg = errorInfo.getErrorMsg();
        }
    }

    public ErrorInfo getErrorConstant() {
        ErrorInfo errorConstant = new ErrorInfo();
        errorConstant.setErrorCode(this.getErrorCode());
        errorConstant.setErrorMsg(this.getErrorMsg());
        return errorConstant;
    }

}
